# 定义和使用线程：使用threading模块
import threading
import time

exitFlag = 0


# 定义一个线程类
class myThread(threading.Thread):
    # 初始化线程
    def __init__(self, threadID, name):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name

    # 定义线程的run方法
    def run(self):
        print("开始线程：" + self.name)
        print_time(self.name)
        print("退出线程：" + self.name)


# 卖票函数
def print_time(threadName):
    count = 0
    while count < 5:
        if exitFlag:
            threadName.exit()
        time.sleep(2)
        print("%s: %s" % (threadName, '卖出了一张票'))
        count += 1


# 1.创建新线程
ticket_window_1 = myThread(1, '售票员1')
ticket_window_2 = myThread(2, '售票员2')
# 2.开启新线程
ticket_window_1.start()
ticket_window_2.start()
# 3.等待线程终止
ticket_window_1.join()
ticket_window_2.join()
print("退出主线程")
